<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <meta http-equiv="Cache-Control" content="no-siteapp">
  <meta name="viewport" content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no">
  <meta name="renderer" content="webkit">

  
  <title>Android 10.0 截屏流程 | 龙之叶的博客</title>

  <link rel="shortcut icon" href="/images/favicon.png">
  <link rel="alternate" href="/atom.xml" title="龙之叶的博客" type="application/atom+xml">
  <meta name="description" content="Android 10.0 截屏流程">
<meta property="og:type" content="article">
<meta property="og:title" content="Android 10.0 截屏流程">
<meta property="og:url" content="http://longzhiye.top/2023/12/16/2023-12-16/index.html">
<meta property="og:site_name" content="龙之叶的博客">
<meta property="og:description" content="Android 10.0 截屏流程">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2023-12-15T16:00:00.000Z">
<meta property="article:modified_time" content="2024-08-29T02:40:34.916Z">
<meta property="article:author" content="龙之叶">
<meta property="article:tag" content="Android">
<meta property="article:tag" content="SystemUI">
<meta property="article:tag" content="framework">
<meta property="article:tag" content="Launcher">
<meta name="twitter:card" content="summary">

  <meta name="keywords" content=",Android,SystemUI,framework,Launcher">
  <meta name="format-detection" content="telephone=no,email=no">
  <meta name="theme-color" content="#9C27B0">

  <meta name="mobile-web-app-capable" content="yes">
  <meta name="application-name" content="龙之叶的博客">
  <meta name="msapplication-starturl" content="http://longzhiye.top/2023/12/16/2023-12-16/">
  <meta name="msapplication-navbutton-color" content="#9C27B0">
  <meta name="apple-mobile-web-app-capable" content="yes">
  <meta name="apple-mobile-web-app-title" content="龙之叶的博客">
  <meta name="apple-mobile-web-app-status-bar-style" content="black">
  <link rel="apple-touch-icon" href="/images/favicon.png">

  
    <link rel="canonical" href="http://longzhiye.top/2023/12/16/2023-12-16/">
  

  
  

  
  
  

  
<link rel="stylesheet" href="/css/mdui.css">
<link rel="stylesheet" href="/css/style.css">

  
<link rel="stylesheet" href="/custom.css">

<meta name="generator" content="Hexo 5.4.2"></head>

<body class="mdui-appbar-with-toolbar mdui-theme-primary-indigo mdui-theme-accent-pink">
  <script>var a=localStorage.getItem("mdui-theme-layout-dark");if(a){document.getElementsByTagName("body")[0].className+=" mdui-theme-layout-dark"};</script>
  <script>if(window.matchMedia&&window.matchMedia("(prefers-color-scheme: dark)").matches){document.getElementsByTagName("body")[0].className+=" mdui-theme-layout-dark"};</script>
  <script>var a=localStorage.getItem("mdui-drawer-close");if(!a){document.getElementsByTagName("body")[0].className+=" mdui-drawer-body-left"};</script>
  <header id="header" class="mdui-appbar mdui-appbar-fixed mdui-appbar-scroll-hide mdui-appbar-inset">
  <div class="mdui-toolbar mdui-color-theme">
    <a href="javascript:;" class="mdui-btn mdui-btn-icon" mdui-drawer="{target: '#sidebar', swipe: true}"><i class="mdui-icon material-icons">menu</i></a>
    <a href="/" class="mdui-typo-headline">龙之叶的博客</a>
    <div class="mdui-toolbar-spacer"></div>
    <a href="javascript:;" class="mdui-btn mdui-btn-icon" mdui-dialog="{target: '#search'}" mdui-tooltip="{content: '搜索'}"><i class="mdui-icon material-icons">search</i></a>
	<!--
    <a href="/atom.xml" class="mdui-btn mdui-btn-icon" mdui-tooltip="{content: 'RSS'}"><i class="mdui-icon material-icons">rss_feed</i></a>
	-->
  </div>
</header>
<div class="mdui-dialog" id="search">
  
    <div class="search-form">
      <input type="search" class="search-form-input" placeholder="请输入关键字">
    </div>
    <div class="search-result" data-resource="/search.xml"></div>
  
</div>

  <aside id="sidebar" class="mdui-drawer mdui-drawer-full-height">
  <script>var a=localStorage.getItem("mdui-drawer-close");if(a){document.getElementById("sidebar").className+=" mdui-drawer-close"};</script>
  <div class="mdui-grid-tile">
    <img src="/images/banner.png" style="height: 160px;">
    <img src="/images/avatar.png" class="avatar-animation" style="position: absolute; top: 10%; left: 24px; width: 64px; height: 64px; border: 2px solid #fff; border-radius: 50%;background-color:#fff;">
    <div class="mdui-grid-tile-actions">
      <div class="mdui-grid-tile-text">
        <div class="mdui-grid-tile-title">龙之叶</div>
        <div class="mdui-grid-tile-subtitle"><i class="mdui-icon material-icons">art_track</i>生命不息，折腾不止</div>
      </div>
      
        <div class="mdui-grid-tile-buttons">
          <a href="mailto:longzhiye163@163.com" class="mdui-btn mdui-btn-icon" mdui-tooltip="{content: 'longzhiye163@163.com'}"><i class="mdui-icon material-icons">email</i></a>
        </div>
      
    </div>
  </div>

  <div class="mdui-list" mdui-collapse="{accordion: true}">
    <a href="/" class="mdui-list-item mdui-ripple">
      <i class="mdui-list-item-icon mdui-icon material-icons mdui-text-color-blue">home</i>
      <div class="mdui-list-item-content">主页</div>
    </a>
    <div class="mdui-collapse-item">
      <script>var a=localStorage.getItem("mdui-collapse-item-0");if(a){document.getElementsByClassName("mdui-collapse-item")[0].className+=" mdui-collapse-item-open"};</script>
      <div class="mdui-collapse-item-header mdui-list-item mdui-ripple">
        <i class="mdui-list-item-icon mdui-icon material-icons mdui-text-color-deep-orange">archive</i>
        <div class="mdui-list-item-content">归档</div>
        <i class="mdui-collapse-item-arrow mdui-icon material-icons">keyboard_arrow_down</i>
      </div>
      <div class="mdui-collapse-item-body mdui-list mdui-list-dense">
        
        <a class="mdui-ripple sidebar_archives-link" href="/archives/2025/07/">七月 2025<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/archives/2025/06/">六月 2025<span class="mdui-ripple sidebar_archives-count">2</span></a><a class="mdui-ripple sidebar_archives-link" href="/archives/2025/03/">三月 2025<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/archives/2025/02/">二月 2025<span class="mdui-ripple sidebar_archives-count">2</span></a><a class="mdui-ripple sidebar_archives-link" href="/archives/2025/01/">一月 2025<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/archives/2024/10/">十月 2024<span class="mdui-ripple sidebar_archives-count">7</span></a><a class="mdui-ripple sidebar_archives-link" href="/archives/2024/09/">九月 2024<span class="mdui-ripple sidebar_archives-count">4</span></a><a class="mdui-ripple sidebar_archives-link" href="/archives/2024/08/">八月 2024<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/archives/2024/03/">三月 2024<span class="mdui-ripple sidebar_archives-count">5</span></a><a class="mdui-ripple sidebar_archives-link" href="/archives/2024/02/">二月 2024<span class="mdui-ripple sidebar_archives-count">11</span></a><a class="mdui-ripple sidebar_archives-link" href="/archives/2024/01/">一月 2024<span class="mdui-ripple sidebar_archives-count">9</span></a><a class="mdui-ripple sidebar_archives-link" href="/archives/2023/12/">十二月 2023<span class="mdui-ripple sidebar_archives-count">10</span></a><a class="mdui-ripple sidebar_archives-link" href="/archives/2023/11/">十一月 2023<span class="mdui-ripple sidebar_archives-count">8</span></a><a class="mdui-ripple sidebar_archives-link" href="/archives/2023/10/">十月 2023<span class="mdui-ripple sidebar_archives-count">8</span></a><a class="mdui-ripple sidebar_archives-link" href="/archives/2023/09/">九月 2023<span class="mdui-ripple sidebar_archives-count">3</span></a><a class="mdui-ripple sidebar_archives-link" href="/archives/2023/08/">八月 2023<span class="mdui-ripple sidebar_archives-count">6</span></a><a class="mdui-ripple sidebar_archives-link" href="/archives/2023/06/">六月 2023<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/archives/2023/05/">五月 2023<span class="mdui-ripple sidebar_archives-count">4</span></a><a class="mdui-ripple sidebar_archives-link" href="/archives/2023/04/">四月 2023<span class="mdui-ripple sidebar_archives-count">2</span></a><a class="mdui-ripple sidebar_archives-link" href="/archives/2023/03/">三月 2023<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/archives/2023/02/">二月 2023<span class="mdui-ripple sidebar_archives-count">2</span></a><a class="mdui-ripple sidebar_archives-link" href="/archives/2022/09/">九月 2022<span class="mdui-ripple sidebar_archives-count">7</span></a><a class="mdui-ripple sidebar_archives-link" href="/archives/2022/08/">八月 2022<span class="mdui-ripple sidebar_archives-count">4</span></a><a class="mdui-ripple sidebar_archives-link" href="/archives/2017/04/">四月 2017<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/archives/2017/03/">三月 2017<span class="mdui-ripple sidebar_archives-count">2</span></a><a class="mdui-ripple sidebar_archives-link" href="/archives/2017/02/">二月 2017<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/archives/2017/01/">一月 2017<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/archives/2016/12/">十二月 2016<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/archives/2016/11/">十一月 2016<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/archives/2016/10/">十月 2016<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/archives/2016/08/">八月 2016<span class="mdui-ripple sidebar_archives-count">1</span></a>
        
      </div>
    </div>
    <div class="mdui-collapse-item">
      <script>var a=localStorage.getItem("mdui-collapse-item-1");if(a){document.getElementsByClassName("mdui-collapse-item")[1].className+=" mdui-collapse-item-open"};</script>
      <div class="mdui-collapse-item-header mdui-list-item mdui-ripple">
        <i class="mdui-list-item-icon mdui-icon material-icons mdui-text-color-green">class</i>
        <div class="mdui-list-item-content">分类</div>
        <i class="mdui-collapse-item-arrow mdui-icon material-icons">keyboard_arrow_down</i>
      </div>
      <div class="mdui-collapse-item-body mdui-list mdui-list-dense">
        
        <a class="mdui-ripple sidebar_archives-link" href="/categories/%E6%8A%80%E6%9C%AF/">技术<span class="mdui-ripple sidebar_archives-count">98</span></a><a class="mdui-ripple sidebar_archives-link" href="/categories/%E7%94%9F%E6%B4%BB/">生活<span class="mdui-ripple sidebar_archives-count">3</span></a><a class="mdui-ripple sidebar_archives-link" href="/categories/%E8%B5%84%E8%AE%AF/">资讯<span class="mdui-ripple sidebar_archives-count">4</span></a><a class="mdui-ripple sidebar_archives-link" href="/categories/%E9%80%9A%E7%9F%A5/">通知<span class="mdui-ripple sidebar_archives-count">4</span></a>
        
      </div>
    </div>
    <div class="mdui-collapse-item">
      <script>var a=localStorage.getItem("mdui-collapse-item-2");if(a){document.getElementsByClassName("mdui-collapse-item")[2].className+=" mdui-collapse-item-open"};</script>
      <div class="mdui-collapse-item-header mdui-list-item mdui-ripple">
        <i class="mdui-list-item-icon mdui-icon material-icons mdui-text-color-brown">bookmark</i>
        <div class="mdui-list-item-content">标签</div>
        <i class="mdui-collapse-item-arrow mdui-icon material-icons">keyboard_arrow_down</i>
      </div>
      <div class="mdui-collapse-item-body mdui-list mdui-list-dense">
        
        <a class="mdui-ripple sidebar_archives-link" href="/tags/ANR/" rel="tag">ANR<span class="mdui-ripple sidebar_archives-count">2</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/APP/" rel="tag">APP<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/Android/" rel="tag">Android<span class="mdui-ripple sidebar_archives-count">89</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/Android12/" rel="tag">Android12<span class="mdui-ripple sidebar_archives-count">3</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/Android13/" rel="tag">Android13<span class="mdui-ripple sidebar_archives-count">2</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/Android14/" rel="tag">Android14<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/AndroidStudio/" rel="tag">AndroidStudio<span class="mdui-ripple sidebar_archives-count">3</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/AutoDraw/" rel="tag">AutoDraw<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/Crash/" rel="tag">Crash<span class="mdui-ripple sidebar_archives-count">2</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/Foxmail/" rel="tag">Foxmail<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/Framework/" rel="tag">Framework<span class="mdui-ripple sidebar_archives-count">20</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/GPS%E8%BD%A8%E8%BF%B9%E5%9B%BE/" rel="tag">GPS轨迹图<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/Git/" rel="tag">Git<span class="mdui-ripple sidebar_archives-count">2</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/GitHub/" rel="tag">GitHub<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/Google/" rel="tag">Google<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/Hexo/" rel="tag">Hexo<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/Java/" rel="tag">Java<span class="mdui-ripple sidebar_archives-count">3</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/Kernel/" rel="tag">Kernel<span class="mdui-ripple sidebar_archives-count">4</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/Launcher/" rel="tag">Launcher<span class="mdui-ripple sidebar_archives-count">17</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/Lib/" rel="tag">Lib<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/Linux/" rel="tag">Linux<span class="mdui-ripple sidebar_archives-count">18</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/Markdown/" rel="tag">Markdown<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/Material-Design/" rel="tag">Material Design<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/OS-X/" rel="tag">OS X<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/OTA/" rel="tag">OTA<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/QuickTime/" rel="tag">QuickTime<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/SSH/" rel="tag">SSH<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/Settings/" rel="tag">Settings<span class="mdui-ripple sidebar_archives-count">6</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/SystemUI/" rel="tag">SystemUI<span class="mdui-ripple sidebar_archives-count">19</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/Ubuntu/" rel="tag">Ubuntu<span class="mdui-ripple sidebar_archives-count">7</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/WPS/" rel="tag">WPS<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/Wifi/" rel="tag">Wifi<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/WindTerm/" rel="tag">WindTerm<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/Windows/" rel="tag">Windows<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/androidmk/" rel="tag">androidmk<span class="mdui-ripple sidebar_archives-count">2</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/blog/" rel="tag">blog<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/build/" rel="tag">build<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/feel/" rel="tag">feel<span class="mdui-ripple sidebar_archives-count">2</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/framework/" rel="tag">framework<span class="mdui-ripple sidebar_archives-count">50</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/git/" rel="tag">git<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/inotify/" rel="tag">inotify<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/life/" rel="tag">life<span class="mdui-ripple sidebar_archives-count">2</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/ninja/" rel="tag">ninja<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/rsync/" rel="tag">rsync<span class="mdui-ripple sidebar_archives-count">2</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/standard/" rel="tag">standard<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/test/" rel="tag">test<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/%E4%BC%81%E4%B8%9A%E5%BE%AE%E4%BF%A1/" rel="tag">企业微信<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/%E6%93%8D%E4%BD%9C%E7%B3%BB%E7%BB%9F/" rel="tag">操作系统<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/%E6%B5%8B%E8%AF%95/" rel="tag">测试<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/%E6%BA%90%E7%A0%81/" rel="tag">源码<span class="mdui-ripple sidebar_archives-count">3</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/%E7%94%9F%E6%B4%BB%E6%84%9F%E6%82%9F/" rel="tag">生活感悟<span class="mdui-ripple sidebar_archives-count">1</span></a>
        
      </div>
    </div>
    <a href="/about" class="mdui-list-item mdui-ripple">
      <i class="mdui-list-item-icon mdui-icon material-icons mdui-text-color-purple">person</i>
      <div class="mdui-list-item-content">关于</div>
    </a>
  </div>

  <div class="mdui-divider"></div>

  <div class="mdui-list" mdui-collapse="{accordion: true}">
    
      <a href="/timeline" class="mdui-list-item mdui-ripple">时间轴</a>
    
    <div class="mdui-collapse-item">
      <script>var a=localStorage.getItem("mdui-collapse-item-3");if(a){document.getElementsByClassName("mdui-collapse-item")[3].className+=" mdui-collapse-item-open"};</script>
      <div class="mdui-collapse-item-header mdui-list-item mdui-ripple">
        <div class="mdui-list-item-content">友情链接</div>
        <i class="mdui-list-item-icon mdui-icon material-icons">link</i>
      </div>
      <div class="mdui-collapse-item-body mdui-list mdui-list-dense">
        
          <a href="http://www.niemingzhao.top" target="_blank" class="mdui-list-item mdui-p-l-2 mdui-text-color-theme-accent mdui-ripple" style="justify-content: center;">聂明照的博客</a>
        
        
      </div>
    </div>
  </div>
</aside>

  <main id="main" class="mdui-m-t-5 fadeIn animated">
  
<link rel="stylesheet" href="//cdn.bootcdn.net/ajax/libs/fancybox/3.5.7/jquery.fancybox.min.css">

  <style>#main article .mdui-card-content .center-block{display:block!important;margin-right:auto!important;margin-left:auto!important}</style>
  <style>#main article .mdui-card-content .text-center{text-align:center!important}</style>
  <article class="mdui-card mdui-m-b-5">
    <header class="mdui-card-media">
      <img src="/images/random/material-6.png" style="max-height: 240px;">
      <div class="mdui-card-media-covered">
        <div class="mdui-card-primary">
          <div class="mdui-card-primary-title">Android 10.0 截屏流程</div>
          <div class="mdui-card-primary-subtitle"><i class="iconfont">&#xe697;</i> 2023-12-16 / <i class="iconfont">&#xe601;</i> 龙之叶 &nbsp;&nbsp; <span id="busuanzi_container_page_pv" style="display: none;"><i class="iconfont">&#xe7fd;</i> <span id="busuanzi_value_page_pv"></span></span></div>
        </div>
      </div>
      <div class="mdui-card-menu">
        
          <button class="mdui-btn mdui-btn-icon mdui-text-color-white" mdui-menu="{target: '#qrcode', align: 'right'}"><i class="mdui-icon material-icons">devices</i></button>
          <ul class="mdui-menu" id="qrcode">
            
              <li class="mdui-menu-item"><a class="mdui-text-center mdui-ripple">发送到手机</a></li>
            
            <li class="mdui-menu-item" disabled>
              
                <img src="//qr.liantu.com/api.php?w=246&m=10&text=http://longzhiye.top/2023/12/16/2023-12-16/">
              
            </li>
          </ul>
        
        
          <button class="mdui-btn mdui-btn-icon mdui-text-color-white" mdui-menu="{target: '#share_menu', align: 'right'}"><i class="mdui-icon material-icons">share</i></button>
          <ul class="mdui-menu" id="share_menu">
            <li class="mdui-menu-item">
              <a href="https://service.weibo.com/share/share.php?appkey=&title=Android 10.0 截屏流程&url=http://longzhiye.top/2023/12/16/2023-12-16/&pic=http://longzhiye.top/images/favicon.png&searchPic=false&style=simple" target="_blank" class="mdui-ripple">分享到 Weibo</a>
            </li>
            <li class="mdui-menu-item">
              <a href="https://twitter.com/intent/tweet?text=Android 10.0 截屏流程&url=http://longzhiye.top/2023/12/16/2023-12-16/&via=龙之叶" target="_blank" class="mdui-ripple">分享到 Twitter</a>
            </li>
            <li class="mdui-menu-item">
              <a href="https://www.facebook.com/sharer/sharer.php?u=http://longzhiye.top/2023/12/16/2023-12-16/" target="_blank" class="mdui-ripple">分享到 Facebook</a>
            </li>
            <li class="mdui-menu-item">
              <a href="https://plus.google.com/share?url=http://longzhiye.top/2023/12/16/2023-12-16/" target="_blank" class="mdui-ripple">分享到 Google+</a>
            </li>
            <li class="mdui-menu-item">
              <a href="https://www.linkedin.com/shareArticle?mini=true&url=http://longzhiye.top/2023/12/16/2023-12-16/&title=Android 10.0 截屏流程" target="_blank" class="mdui-ripple">分享到 LinkedIn</a>
            </li>
            <li class="mdui-menu-item">
              <a href="https://connect.qq.com/widget/shareqq/index.html?site=龙之叶的博客&title=Android 10.0 截屏流程&pics=http://longzhiye.top/images/favicon.png&url=http://longzhiye.top/2023/12/16/2023-12-16/" target="_blank" class="mdui-ripple">分享到 QQ</a>
            </li>
            <li class="mdui-menu-item">
              <a href="https://telegram.me/share/url?url=http://longzhiye.top/2023/12/16/2023-12-16/&text=Android 10.0 截屏流程" target="_blank" class="mdui-ripple">分享到 Telegram</a>
            </li>
          </ul>
        
      </div>
    </header>
    <div class="mdui-card-content mdui-typo">
      <p>通常未通过特殊定制的 Android 系统，截屏都是经过同时按住音量下键和电源键来截屏。本篇文章就只讨论使用这些特殊按键来进行截屏。<br>这里我们就要明白事件是在哪里进行分发拦截的。通过源码的分析，我们发现是在PhoneWindowManager.java 中。<br>PhoneWindowManager#interceptKeyBeforeQueueing()<br><strong>frameworks/base/services/core/java/com/android/server/policy/PhoneWindowManager.java</strong></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Override</span></span><br><span class="line"><span class="keyword">public</span> <span class="type">int</span> <span class="title function_">interceptKeyBeforeQueueing</span><span class="params">(KeyEvent event, <span class="type">int</span> policyFlags)</span> &#123;</span><br><span class="line">    <span class="keyword">if</span> (!mSystemBooted) &#123;</span><br><span class="line">        <span class="comment">// If we have not yet booted, don&#x27;t let key events do anything.</span></span><br><span class="line">        <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">// 省略部分代码......</span></span><br><span class="line">    <span class="comment">// Handle special keys.</span></span><br><span class="line">    <span class="keyword">switch</span> (keyCode) &#123;</span><br><span class="line">         ......</span><br><span class="line">        <span class="keyword">case</span> KeyEvent.KEYCODE_VOLUME_DOWN:</span><br><span class="line">        <span class="keyword">case</span> KeyEvent.KEYCODE_VOLUME_UP:</span><br><span class="line">        <span class="keyword">case</span> KeyEvent.KEYCODE_VOLUME_MUTE: &#123;</span><br><span class="line">            <span class="comment">// 按下音量键调用</span></span><br><span class="line">            handleVolumeKey(event, policyFlags);</span><br><span class="line">            ......</span><br><span class="line">            <span class="keyword">break</span>;</span><br><span class="line">        &#125;</span><br><span class="line">        ......</span><br><span class="line">        <span class="keyword">case</span> KeyEvent.KEYCODE_POWER: &#123;</span><br><span class="line">            ......</span><br><span class="line">            <span class="keyword">if</span> (down) &#123;</span><br><span class="line">                <span class="comment">// 按下电源键将调用</span></span><br><span class="line">                interceptPowerKeyDown(event, interactive);</span><br><span class="line">            &#125; <span class="keyword">else</span> &#123;          </span><br><span class="line">                interceptPowerKeyUp(event, interactive, canceled);</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="keyword">break</span>;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> result;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="1、电源键处理"><a href="#1、电源键处理" class="headerlink" title="1、电源键处理"></a>1、电源键处理</h3><p>PhoneWindowManager#interceptPowerKeyDown()<br><strong>frameworks/base/services/core/java/com/android/server/policy/PhoneWindowManager.java</strong></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">private</span> <span class="keyword">void</span> <span class="title function_">interceptPowerKeyDown</span><span class="params">(KeyEvent event, <span class="type">boolean</span> interactive)</span> &#123;</span><br><span class="line">    <span class="comment">// 省略部分代码......</span></span><br><span class="line">    <span class="comment">// Latch power key state to detect screenshot chord.</span></span><br><span class="line">    <span class="keyword">if</span> (interactive &amp;&amp; !mScreenshotChordPowerKeyTriggered</span><br><span class="line">            &amp;&amp; (event.getFlags() &amp; KeyEvent.FLAG_FALLBACK) == <span class="number">0</span>) &#123;</span><br><span class="line">        <span class="comment">// power键按下的标志</span></span><br><span class="line">        mScreenshotChordPowerKeyTriggered = <span class="literal">true</span>;</span><br><span class="line">        <span class="comment">// 获取 Power 键的触发时间</span></span><br><span class="line">        mScreenshotChordPowerKeyTime = event.getDownTime();</span><br><span class="line">        <span class="comment">// 处理屏幕截图事件</span></span><br><span class="line">        interceptScreenshotChord();</span><br><span class="line">        <span class="comment">// 这个方法应该是消耗、拦截事件的，避免改变音量、铃声等。</span></span><br><span class="line">        interceptRingerToggleChord();</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">// 省略部分代码......</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>interceptScreenshotChord()该方法下面再说，先介绍电源按键、音量按键的处理。</p>
<h3 id="2、音量键处理"><a href="#2、音量键处理" class="headerlink" title="2、音量键处理"></a>2、音量键处理</h3><p>PhoneWindowManager#handleVolumeKey()<br><strong>frameworks/base/services/core/java/com/android/server/policy/PhoneWindowManager.java</strong></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">handleVolumeKey</span><span class="params">(KeyEvent event, <span class="type">int</span> policyFlags)</span> &#123;</span><br><span class="line">    <span class="keyword">final</span> <span class="type">boolean</span> <span class="variable">down</span> <span class="operator">=</span> event.getAction() == KeyEvent.ACTION_DOWN;</span><br><span class="line">    <span class="keyword">final</span> <span class="type">int</span> <span class="variable">keyCode</span> <span class="operator">=</span> event.getKeyCode();</span><br><span class="line">    <span class="keyword">final</span> <span class="type">boolean</span> <span class="variable">interactive</span> <span class="operator">=</span> (policyFlags &amp; FLAG_INTERACTIVE) != <span class="number">0</span>;</span><br><span class="line">    <span class="keyword">if</span> (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN) &#123;</span><br><span class="line">        <span class="keyword">if</span> (down) &#123;</span><br><span class="line">            <span class="comment">// Any activity on the vol down button stops the ringer toggle shortcut</span></span><br><span class="line">            cancelPendingRingerToggleChordAction();</span><br><span class="line">            <span class="keyword">if</span> (interactive &amp;&amp; !mScreenshotChordVolumeDownKeyTriggered</span><br><span class="line">                    &amp;&amp; (event.getFlags() &amp; KeyEvent.FLAG_FALLBACK) == <span class="number">0</span>) &#123;</span><br><span class="line">                <span class="comment">// Volume键按下的标志</span></span><br><span class="line">                mScreenshotChordVolumeDownKeyTriggered = <span class="literal">true</span>;</span><br><span class="line">                <span class="comment">// 获取 Volume 键的触发时间</span></span><br><span class="line">                mScreenshotChordVolumeDownKeyTime = event.getDownTime();</span><br><span class="line">                <span class="comment">// 赋值  false 该属性为了防止截屏的时候音量下键生效出现调节音量的 dialog 状态值</span></span><br><span class="line">                mScreenshotChordVolumeDownKeyConsumed = <span class="literal">false</span>;</span><br><span class="line">                <span class="comment">// 防止触发 Power  键长按功能</span></span><br><span class="line">                cancelPendingPowerKeyAction();</span><br><span class="line">                <span class="comment">//处理屏幕截图事件</span></span><br><span class="line">                interceptScreenshotChord();</span><br><span class="line">                <span class="comment">// 拦截相关快捷键</span></span><br><span class="line">                interceptAccessibilityShortcutChord();</span><br><span class="line">            &#125;</span><br><span class="line">        &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">             <span class="comment">// 省略部分代码......</span></span><br><span class="line">        &#125;</span><br><span class="line">    &#125; <span class="keyword">else</span> <span class="keyword">if</span> (keyCode == KeyEvent.KEYCODE_VOLUME_UP) &#123;</span><br><span class="line">         <span class="comment">// 省略部分代码......</span></span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="3、截屏事件处理-interceptScreenshotChord"><a href="#3、截屏事件处理-interceptScreenshotChord" class="headerlink" title="3、截屏事件处理 interceptScreenshotChord()"></a>3、截屏事件处理 interceptScreenshotChord()</h3><p>PhoneWindowManager#interceptScreenshotChord()<br><strong>frameworks/base/services/core/java/com/android/server/policy/PhoneWindowManager.java</strong></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">private</span> <span class="keyword">void</span> <span class="title function_">interceptScreenshotChord</span><span class="params">()</span> &#123;</span><br><span class="line">      <span class="comment">/*</span></span><br><span class="line"><span class="comment">       * if 判断参数介绍</span></span><br><span class="line"><span class="comment">       * mScreenshotChordEnabled 其值为mContext.getResources().getBoolean(com.android.internal.R.bool.config_enableScreenshotChord);</span></span><br><span class="line"><span class="comment">       * mScreenshotChordVolumeDownKeyTriggered 音量下键按下时值为true</span></span><br><span class="line"><span class="comment">       * mScreenshotChordPowerKeyTriggered  电源键按下时值为true</span></span><br><span class="line"><span class="comment">       * mA11yShortcutChordVolumeUpKeyTriggered 音量上(加)键抬起时为false , 按下时为true</span></span><br><span class="line"><span class="comment">       **/</span></span><br><span class="line">    <span class="keyword">if</span> (mScreenshotChordEnabled</span><br><span class="line">            &amp;&amp; mScreenshotChordVolumeDownKeyTriggered &amp;&amp; mScreenshotChordPowerKeyTriggered</span><br><span class="line">            &amp;&amp; !mA11yShortcutChordVolumeUpKeyTriggered) &#123;</span><br><span class="line">        <span class="comment">// 获取当前时间</span></span><br><span class="line">        <span class="keyword">final</span> <span class="type">long</span> <span class="variable">now</span> <span class="operator">=</span> SystemClock.uptimeMillis();</span><br><span class="line">        <span class="comment">// 当前时间小于 音量下键按下时间 + 150ms</span></span><br><span class="line">        <span class="comment">// 当前时间小于 power键按下时间 + 150ms</span></span><br><span class="line">        <span class="keyword">if</span> (now &lt;= mScreenshotChordVolumeDownKeyTime + SCREENSHOT_CHORD_DEBOUNCE_DELAY_MILLIS</span><br><span class="line">                &amp;&amp; now &lt;= mScreenshotChordPowerKeyTime</span><br><span class="line">                + SCREENSHOT_CHORD_DEBOUNCE_DELAY_MILLIS) &#123;</span><br><span class="line">            <span class="type">boolean</span> <span class="variable">inLongScreenshot</span> <span class="operator">=</span> Settings.System.getIntForUser(mContext.getContentResolver(),</span><br><span class="line">                    LONGSCREENSHOT_SETTING, <span class="number">0</span>, UserHandle.USER_CURRENT_OR_SELF) == <span class="number">1</span>;</span><br><span class="line">            <span class="keyword">if</span> (hasInPowerUtrlSavingMode() || inLongScreenshot) &#123;</span><br><span class="line">                <span class="keyword">return</span>;</span><br><span class="line">            &#125;</span><br><span class="line">  </span><br><span class="line">            <span class="comment">// 长按音量下键，达到截屏条件，将该事件消费掉。</span></span><br><span class="line">            mScreenshotChordVolumeDownKeyConsumed = <span class="literal">true</span>;</span><br><span class="line">            <span class="comment">// 防止触发 Power  键长按功能</span></span><br><span class="line">            cancelPendingPowerKeyAction();</span><br><span class="line">            <span class="comment">// 设置截图的类型，TAKE_SCREENSHOT_FULLSCREEN 为全屏</span></span><br><span class="line">            mScreenshotRunnable.setScreenshotType(TAKE_SCREENSHOT_FULLSCREEN);</span><br><span class="line">            <span class="comment">// 截图的方式，(例如：按键、三指下滑 等等)</span></span><br><span class="line">            mScreenshotRunnable.setScreenshotSource(SCREENSHOT_KEY_CHORD);</span><br><span class="line">            <span class="comment">//执行 mScreenshotRunnable</span></span><br><span class="line">            mHandler.postDelayed(mScreenshotRunnable, getScreenshotChordLongPressDelay());</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>继续查看ScreenshotRunnable，此时会一步步向下调用，最终到SystemUI。<br><strong>frameworks/base/services/core/java/com/android/server/policy/PhoneWindowManager.java</strong></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">private</span> <span class="keyword">class</span> <span class="title class_">ScreenshotRunnable</span> <span class="keyword">implements</span> <span class="title class_">Runnable</span> &#123;</span><br><span class="line">    <span class="keyword">private</span> <span class="type">int</span> <span class="variable">mScreenshotType</span> <span class="operator">=</span> TAKE_SCREENSHOT_FULLSCREEN;</span><br><span class="line">    <span class="keyword">private</span> <span class="type">int</span> <span class="variable">mScreenshotSource</span> <span class="operator">=</span> SCREENSHOT_KEY_OTHER;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">setScreenshotType</span><span class="params">(<span class="type">int</span> screenshotType)</span> &#123;</span><br><span class="line">        mScreenshotType = screenshotType;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">setScreenshotSource</span><span class="params">(<span class="type">int</span> screenshotSource)</span> &#123;</span><br><span class="line">        mScreenshotSource = screenshotSource;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">run</span><span class="params">()</span> &#123;</span><br><span class="line">        <span class="comment">// 回调到 DisplayPolicy.java</span></span><br><span class="line">        mDefaultDisplayPolicy.takeScreenshot(mScreenshotType, mScreenshotSource);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>DisplayPolicy#takeScreenshot()<br><strong>frameworks/base/services/core/java/com/android/server/wm/DisplayPolicy.java</strong></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 请求截取屏幕截图</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">takeScreenshot</span><span class="params">(<span class="type">int</span> screenshotType, <span class="type">int</span> source)</span> &#123;</span><br><span class="line">    <span class="keyword">if</span> (mScreenshotHelper != <span class="literal">null</span>) &#123;</span><br><span class="line">        mScreenshotHelper.takeScreenshot(screenshotType,</span><br><span class="line">                mStatusBar != <span class="literal">null</span> &amp;&amp; mStatusBar.isVisibleLw(),</span><br><span class="line">                mNavigationBar != <span class="literal">null</span> &amp;&amp; mNavigationBar.isVisibleLw(),</span><br><span class="line">                source, mHandler, <span class="literal">null</span> <span class="comment">/* completionConsumer */</span>);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>继续往下看ScreenshotHelper#takeScreenshot()<br><strong>frameworks/base/core/java/com/android/internal/util/ScreenshotHelper.java</strong></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 请求截取屏幕截图</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">takeScreenshot</span><span class="params">(<span class="keyword">final</span> <span class="type">int</span> screenshotType, <span class="keyword">final</span> <span class="type">boolean</span> hasStatus,</span></span><br><span class="line"><span class="params">        <span class="keyword">final</span> <span class="type">boolean</span> hasNav, <span class="type">int</span> source, <span class="meta">@NonNull</span> Handler handler,</span></span><br><span class="line"><span class="params">        <span class="meta">@Nullable</span> Consumer&lt;Uri&gt; completionConsumer)</span> &#123;</span><br><span class="line">    <span class="type">ScreenshotRequest</span> <span class="variable">screenshotRequest</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">ScreenshotRequest</span>(source, hasStatus, hasNav);</span><br><span class="line">    takeScreenshot(screenshotType, SCREENSHOT_TIMEOUT_MS, handler, screenshotRequest,</span><br><span class="line">            completionConsumer);</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">//到了 Binder调用环节, 此为客户端, 服务端为SystemUI中的 TakeScreenshotService</span></span><br><span class="line"><span class="keyword">private</span> <span class="keyword">void</span> <span class="title function_">takeScreenshot</span><span class="params">(<span class="keyword">final</span> <span class="type">int</span> screenshotType, <span class="type">long</span> timeoutMs, <span class="meta">@NonNull</span> Handler handler,</span></span><br><span class="line"><span class="params">            ScreenshotRequest screenshotRequest, <span class="meta">@Nullable</span> Consumer&lt;Uri&gt; completionConsumer)</span> &#123;</span><br><span class="line">        <span class="keyword">synchronized</span> (mScreenshotLock) &#123;</span><br><span class="line">            <span class="keyword">final</span> <span class="type">Runnable</span> <span class="variable">mScreenshotTimeout</span> <span class="operator">=</span> () -&gt; &#123;</span><br><span class="line">                <span class="keyword">synchronized</span> (mScreenshotLock) &#123;</span><br><span class="line">                    <span class="keyword">if</span> (mScreenshotConnection != <span class="literal">null</span>) &#123;</span><br><span class="line">                        <span class="comment">// 在获取屏幕截图捕获响应之前超时</span></span><br><span class="line">                        Log.e(TAG, <span class="string">&quot;Timed out before getting screenshot capture response&quot;</span>);</span><br><span class="line">                        <span class="comment">// 重置连接</span></span><br><span class="line">                        resetConnection();</span><br><span class="line">                        <span class="comment">// 通知截屏错误</span></span><br><span class="line">                        notifyScreenshotError();</span><br><span class="line">                    &#125;</span><br><span class="line">                &#125;</span><br><span class="line">                <span class="keyword">if</span> (completionConsumer != <span class="literal">null</span>) &#123;</span><br><span class="line">                    completionConsumer.accept(<span class="literal">null</span>);</span><br><span class="line">                &#125;</span><br><span class="line">            &#125;;</span><br><span class="line">            <span class="type">Message</span> <span class="variable">msg</span> <span class="operator">=</span> Message.obtain(<span class="literal">null</span>, screenshotType, screenshotRequest);</span><br><span class="line">            <span class="type">Handler</span> <span class="variable">h</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">Handler</span>(handler.getLooper()) &#123;</span><br><span class="line">                <span class="meta">@Override</span></span><br><span class="line">                <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">handleMessage</span><span class="params">(Message msg)</span> &#123;</span><br><span class="line">                    <span class="keyword">switch</span> (msg.what) &#123;</span><br><span class="line">                        <span class="keyword">case</span> SCREENSHOT_MSG_URI:</span><br><span class="line">                            <span class="keyword">if</span> (completionConsumer != <span class="literal">null</span>) &#123;</span><br><span class="line">                                completionConsumer.accept((Uri) msg.obj);</span><br><span class="line">                            &#125;</span><br><span class="line">                            handler.removeCallbacks(mScreenshotTimeout);</span><br><span class="line">                            <span class="keyword">break</span>;</span><br><span class="line">                        <span class="keyword">case</span> SCREENSHOT_MSG_PROCESS_COMPLETE:</span><br><span class="line">                            <span class="keyword">synchronized</span> (mScreenshotLock) &#123;</span><br><span class="line">                                resetConnection();</span><br><span class="line">                            &#125;</span><br><span class="line">                            <span class="keyword">break</span>;</span><br><span class="line">                    &#125;</span><br><span class="line">                &#125;</span><br><span class="line">            &#125;;</span><br><span class="line">            msg.replyTo = <span class="keyword">new</span> <span class="title class_">Messenger</span>(h);</span><br><span class="line">            <span class="keyword">if</span> (mScreenshotConnection == <span class="literal">null</span> || mScreenshotService == <span class="literal">null</span>) &#123;</span><br><span class="line">                <span class="comment">// 一个标准的Service连接</span></span><br><span class="line">                <span class="comment">// config_screenshotServiceComponent == com.android.systemui/com.android.systemui.screenshot.TakeScreenshotService</span></span><br><span class="line">                <span class="keyword">final</span> <span class="type">ComponentName</span> <span class="variable">serviceComponent</span> <span class="operator">=</span> ComponentName.unflattenFromString(</span><br><span class="line">                        mContext.getResources().getString(</span><br><span class="line">                                com.android.internal.R.string.config_screenshotServiceComponent));</span><br><span class="line">                <span class="keyword">final</span> <span class="type">Intent</span> <span class="variable">serviceIntent</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">Intent</span>();</span><br><span class="line">                serviceIntent.setComponent(serviceComponent);</span><br><span class="line">                <span class="type">ServiceConnection</span> <span class="variable">conn</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">ServiceConnection</span>() &#123;</span><br><span class="line">                    <span class="meta">@Override</span></span><br><span class="line">                    <span class="comment">// 当Service连接成功之后</span></span><br><span class="line">                    <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">onServiceConnected</span><span class="params">(ComponentName name, IBinder service)</span> &#123;</span><br><span class="line">                        <span class="keyword">synchronized</span> (mScreenshotLock) &#123;</span><br><span class="line">                            <span class="keyword">if</span> (mScreenshotConnection != <span class="built_in">this</span>) &#123;</span><br><span class="line">                                <span class="keyword">return</span>;</span><br><span class="line">                            &#125;</span><br><span class="line">                            mScreenshotService = service;</span><br><span class="line">                            <span class="type">Messenger</span> <span class="variable">messenger</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">Messenger</span>(mScreenshotService);</span><br><span class="line">                            <span class="keyword">try</span> &#123;</span><br><span class="line">                                messenger.send(msg);</span><br><span class="line">                            &#125; <span class="keyword">catch</span> (RemoteException e) &#123;</span><br><span class="line">                                Log.e(TAG, <span class="string">&quot;Couldn&#x27;t take screenshot: &quot;</span> + e);</span><br><span class="line">                                <span class="keyword">if</span> (completionConsumer != <span class="literal">null</span>) &#123;</span><br><span class="line">                                    completionConsumer.accept(<span class="literal">null</span>);</span><br><span class="line">                                &#125;</span><br><span class="line">                            &#125;</span><br><span class="line">                        &#125;</span><br><span class="line">                    &#125;</span><br><span class="line">                    <span class="meta">@Override</span></span><br><span class="line">                    <span class="comment">// 当Service断开连接时</span></span><br><span class="line">                    <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">onServiceDisconnected</span><span class="params">(ComponentName name)</span> &#123;</span><br><span class="line">                        <span class="keyword">synchronized</span> (mScreenshotLock) &#123;</span><br><span class="line">                            <span class="keyword">if</span> (mScreenshotConnection != <span class="literal">null</span>) &#123;</span><br><span class="line">                                resetConnection();</span><br><span class="line">                                <span class="comment">// only log an error if we&#x27;re still within the timeout period</span></span><br><span class="line">                                <span class="keyword">if</span> (handler.hasCallbacks(mScreenshotTimeout)) &#123;</span><br><span class="line">                                    handler.removeCallbacks(mScreenshotTimeout);</span><br><span class="line">                                    notifyScreenshotError();</span><br><span class="line">                                &#125;</span><br><span class="line">                            &#125;</span><br><span class="line">                        &#125;</span><br><span class="line">                    &#125;</span><br><span class="line">                &#125;;</span><br><span class="line">                <span class="comment">// bindService</span></span><br><span class="line">                <span class="keyword">if</span> (mContext.bindServiceAsUser(serviceIntent, conn,</span><br><span class="line">                        Context.BIND_AUTO_CREATE | Context.BIND_FOREGROUND_SERVICE,</span><br><span class="line">                        UserHandle.CURRENT)) &#123;</span><br><span class="line">                    mScreenshotConnection = conn;</span><br><span class="line">                    handler.postDelayed(mScreenshotTimeout, timeoutMs);</span><br><span class="line">                &#125;</span><br><span class="line">            &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">                <span class="comment">// 如果已经连接则直接发送Message</span></span><br><span class="line">                <span class="type">Messenger</span> <span class="variable">messenger</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">Messenger</span>(mScreenshotService);</span><br><span class="line">                <span class="keyword">try</span> &#123;</span><br><span class="line">                    messenger.send(msg);</span><br><span class="line">                &#125; <span class="keyword">catch</span> (RemoteException e) &#123;</span><br><span class="line">                    Log.e(TAG, <span class="string">&quot;Couldn&#x27;t take screenshot: &quot;</span> + e);</span><br><span class="line">                    <span class="keyword">if</span> (completionConsumer != <span class="literal">null</span>) &#123;</span><br><span class="line">                        completionConsumer.accept(<span class="literal">null</span>);</span><br><span class="line">                    &#125;</span><br><span class="line">                &#125;</span><br><span class="line">                handler.postDelayed(mScreenshotTimeout, timeoutMs);</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>客户端通过向服务端发送 message 来将截屏任务交给 service，由 service 处理后面的操作。<br><strong>frameworks/base/packages/SystemUI/src/com/android/systemui/screenshot/TakeScreenshotService.java</strong></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">private</span> <span class="type">Handler</span> <span class="variable">mHandler</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">Handler</span>(Looper.myLooper()) &#123;</span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">handleMessage</span><span class="params">(Message msg)</span> &#123;</span><br><span class="line">        <span class="comment">// 获取客户端传的 Messenger 对象</span></span><br><span class="line">        <span class="keyword">final</span> <span class="type">Messenger</span> <span class="variable">callback</span> <span class="operator">=</span> msg.replyTo;</span><br><span class="line">        Consumer&lt;Uri&gt; uriConsumer = uri -&gt; &#123;</span><br><span class="line">            <span class="type">Message</span> <span class="variable">reply</span> <span class="operator">=</span> Message.obtain(<span class="literal">null</span>, SCREENSHOT_MSG_URI, uri);</span><br><span class="line">            <span class="keyword">try</span> &#123;</span><br><span class="line">                / /Messenger 双向通信，在服务端用远程客户端的 Messenger 对象给客户端发送信息</span><br><span class="line">                callback.send(reply);</span><br><span class="line">            &#125; <span class="keyword">catch</span> (RemoteException e) &#123;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;;</span><br><span class="line">        <span class="type">Runnable</span> <span class="variable">onComplete</span> <span class="operator">=</span> () -&gt; &#123;</span><br><span class="line">            <span class="type">Message</span> <span class="variable">reply</span> <span class="operator">=</span> Message.obtain(<span class="literal">null</span>, SCREENSHOT_MSG_PROCESS_COMPLETE);</span><br><span class="line">            <span class="keyword">try</span> &#123;</span><br><span class="line">                callback.send(reply);</span><br><span class="line">            &#125; <span class="keyword">catch</span> (RemoteException e) &#123;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;;</span><br><span class="line"></span><br><span class="line">        <span class="comment">// 判断用户的设备是否为解锁状态</span></span><br><span class="line">        <span class="comment">// 如果用户的存储被锁定，我们没有地方存储截图，所以跳过它，而不是显示一个误导性的动画和错误通知。</span></span><br><span class="line">        <span class="keyword">if</span> (!mUserManager.isUserUnlocked()) &#123;</span><br><span class="line">            Log.w(TAG, <span class="string">&quot;Skipping screenshot because storage is locked!&quot;</span>);</span><br><span class="line">            post(() -&gt; uriConsumer.accept(<span class="literal">null</span>));</span><br><span class="line">            post(onComplete);</span><br><span class="line">            <span class="keyword">return</span>;</span><br><span class="line">        &#125;</span><br><span class="line">        ScreenshotHelper.<span class="type">ScreenshotRequest</span> <span class="variable">screenshotRequest</span> <span class="operator">=</span></span><br><span class="line">                (ScreenshotHelper.ScreenshotRequest) msg.obj;</span><br><span class="line">        mUiEventLogger.log(ScreenshotEvent.getScreenshotSource(screenshotRequest.getSource()));</span><br><span class="line">        <span class="keyword">switch</span> (msg.what) &#123;</span><br><span class="line">            <span class="keyword">case</span> WindowManager.TAKE_SCREENSHOT_FULLSCREEN:  <span class="comment">// 全屏截图</span></span><br><span class="line">                <span class="comment">// 我们在PhoneWindowManager传入的type为全屏截图,所以需要执行全屏截图流程</span></span><br><span class="line">                mScreenshot.takeScreenshot(uriConsumer, onComplete);</span><br><span class="line">                <span class="keyword">break</span>;</span><br><span class="line">            <span class="keyword">case</span> WindowManager.TAKE_SCREENSHOT_SELECTED_REGION:  <span class="comment">// 区域截图</span></span><br><span class="line">                mScreenshot.takeScreenshot(uriConsumer, onComplete);</span><br><span class="line">                <span class="keyword">break</span>;</span><br><span class="line">            <span class="keyword">case</span> WindowManager.TAKE_SCREENSHOT_PROVIDED_IMAGE:</span><br><span class="line">                <span class="type">Bitmap</span> <span class="variable">screenshot</span> <span class="operator">=</span> BitmapUtil.bundleToHardwareBitmap(</span><br><span class="line">                        screenshotRequest.getBitmapBundle());</span><br><span class="line">                <span class="type">Rect</span> <span class="variable">screenBounds</span> <span class="operator">=</span> screenshotRequest.getBoundsInScreen();</span><br><span class="line">                <span class="type">Insets</span> <span class="variable">insets</span> <span class="operator">=</span> screenshotRequest.getInsets();</span><br><span class="line">                <span class="type">int</span> <span class="variable">taskId</span> <span class="operator">=</span> screenshotRequest.getTaskId();</span><br><span class="line">                <span class="type">int</span> <span class="variable">userId</span> <span class="operator">=</span> screenshotRequest.getUserId();</span><br><span class="line">                <span class="type">ComponentName</span> <span class="variable">topComponent</span> <span class="operator">=</span> screenshotRequest.getTopComponent();</span><br><span class="line">                mScreenshot.handleImageAsScreenshot(screenshot, screenBounds, insets,</span><br><span class="line">                        taskId, userId, topComponent, uriConsumer, onComplete);</span><br><span class="line">                <span class="keyword">break</span>;</span><br><span class="line">            <span class="keyword">default</span>:</span><br><span class="line">                Log.d(TAG, <span class="string">&quot;Invalid screenshot option: &quot;</span> + msg.what);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>
<p>TakeScreenshotService调用GlobalScreenshot.java的takeScreenshot()；<br>GlobalScreenshot#takeScreenshot()</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// GlobalScreenshot.java</span></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     *截取当前显示的屏幕截图并显示动画。.</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">void</span> <span class="title function_">takeScreenshot</span><span class="params">(Consumer&lt;Uri&gt; finisher, Rect crop)</span> &#123;</span><br><span class="line">        <span class="comment">// copy the input Rect, since SurfaceControl.screenshot can mutate it</span></span><br><span class="line">        <span class="type">Rect</span> <span class="variable">screenRect</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">Rect</span>(crop);</span><br><span class="line">        <span class="type">int</span> <span class="variable">rot</span> <span class="operator">=</span> mDisplay.getRotation();</span><br><span class="line">        <span class="type">int</span> <span class="variable">width</span> <span class="operator">=</span> crop.width();</span><br><span class="line">        <span class="type">int</span> <span class="variable">height</span> <span class="operator">=</span> crop.height();</span><br><span class="line">        takeScreenshot(SurfaceControl.screenshot(crop, width, height, rot), finisher, screenRect,</span><br><span class="line">                Insets.NONE, <span class="literal">true</span>);</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">void</span> <span class="title function_">takeScreenshot</span><span class="params">(Bitmap screenshot, Consumer&lt;Uri&gt; finisher, Rect screenRect,</span></span><br><span class="line"><span class="params">            Insets screenInsets, <span class="type">boolean</span> showFlash)</span> &#123;</span><br><span class="line">        <span class="comment">// 此方法会清除上一次的截图信息--连续截图行为</span></span><br><span class="line">        dismissScreenshot(<span class="string">&quot;new screenshot requested&quot;</span>, <span class="literal">true</span>);</span><br><span class="line">        mScreenBitmap = screenshot;</span><br><span class="line">        <span class="keyword">if</span> (mScreenBitmap == <span class="literal">null</span>) &#123;</span><br><span class="line">            <span class="comment">// 如果没有Bitmap则报告错误信息</span></span><br><span class="line">            mNotificationsController.notifyScreenshotError(</span><br><span class="line">                    R.string.screenshot_failed_to_capture_text);</span><br><span class="line">            finisher.accept(<span class="literal">null</span>);</span><br><span class="line">            mOnCompleteRunnable.run();</span><br><span class="line">            <span class="keyword">return</span>;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">if</span> (!isUserSetupComplete()) &#123;</span><br><span class="line">            <span class="comment">// 用户设置尚未完成,不应该向用户展示 分享和编辑 , 只显示一个Toast并保存图片</span></span><br><span class="line">            saveScreenshotAndToast(finisher);</span><br><span class="line">            <span class="keyword">return</span>;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="comment">// Optimizations</span></span><br><span class="line">        mScreenBitmap.setHasAlpha(<span class="literal">false</span>);</span><br><span class="line">        mScreenBitmap.prepareToDraw();</span><br><span class="line">        onConfigChanged(mContext.getResources().getConfiguration());</span><br><span class="line">        <span class="keyword">if</span> (mDismissAnimation != <span class="literal">null</span> &amp;&amp; mDismissAnimation.isRunning()) &#123;</span><br><span class="line">            mDismissAnimation.cancel();</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="comment">// 获取焦点</span></span><br><span class="line">        setWindowFocusable(<span class="literal">true</span>);</span><br><span class="line">        <span class="comment">// 开始截图后动画</span></span><br><span class="line">        startAnimation(finisher, screenRect, screenInsets, showFlash);</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 截屏后开始动画</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">void</span> <span class="title function_">startAnimation</span><span class="params">(<span class="keyword">final</span> Consumer&lt;Uri&gt; finisher, Rect screenRect, Insets screenInsets,</span></span><br><span class="line"><span class="params">            <span class="type">boolean</span> showFlash)</span> &#123;</span><br><span class="line">        <span class="keyword">if</span> (mScreenshotIng == <span class="literal">false</span>) &#123;<span class="comment">//unisoc: Modify for bug1360276</span></span><br><span class="line">            mScreenshotIng = <span class="literal">true</span>;</span><br><span class="line">     </span><br><span class="line">            <span class="comment">// 如果开启了省电模式，显示 toast，以便有一些视觉提示已截取屏幕截图</span></span><br><span class="line">            <span class="type">PowerManager</span> <span class="variable">powerManager</span> <span class="operator">=</span>(PowerManager) mContext . getSystemService (Context.POWER_SERVICE);</span><br><span class="line">            <span class="keyword">if</span> (powerManager.isPowerSaveMode()) &#123;</span><br><span class="line">                Toast.makeText(mContext, R.string.screenshot_saved_title, Toast.LENGTH_SHORT).show();</span><br><span class="line">            &#125;</span><br><span class="line">            mScreenshotHandler.post(() -&gt; &#123;</span><br><span class="line">                <span class="keyword">if</span> (!mScreenshotLayout.isAttachedToWindow()) &#123;</span><br><span class="line">                    <span class="comment">// mScreenshotLayout是截屏的缩略图的父View</span></span><br><span class="line">                    <span class="comment">// mScreenshotLayout 在 GlobalScreenshot.java 的构造方法中初始化。对应布局文件：global_screenshot.xml</span></span><br><span class="line">                    mWindowManager.addView(mScreenshotLayout, mWindowLayoutParams);</span><br><span class="line">                &#125;</span><br><span class="line">                <span class="comment">// 动画相关的View</span></span><br><span class="line">                mScreenshotAnimatedView.setImageDrawable(</span><br><span class="line">                        createScreenDrawable(mScreenBitmap, screenInsets));</span><br><span class="line">                setAnimatedViewSize(screenRect.width(), screenRect.height());</span><br><span class="line">                <span class="comment">// 显示动画何时开始</span></span><br><span class="line">                mScreenshotAnimatedView.setVisibility(View.GONE);</span><br><span class="line">                <span class="comment">//缩略图显示的View,将native层返回的Bitmap加载到此View上</span></span><br><span class="line">                mScreenshotPreview.setImageDrawable(createScreenDrawable(mScreenBitmap, screenInsets));</span><br><span class="line">                <span class="comment">// 使静态预览不可见（消失），以便我们可以在屏幕上查询其位置</span></span><br><span class="line">                mScreenshotPreview.setVisibility(View.INVISIBLE);</span><br><span class="line">                mScreenshotHandler.post(() -&gt; &#123;</span><br><span class="line">                mScreenshotLayout.getViewTreeObserver().addOnComputeInternalInsetsListener(<span class="built_in">this</span>);</span><br><span class="line">                <span class="comment">// 创建动画</span></span><br><span class="line">                mScreenshotAnimation =</span><br><span class="line">                        createScreenshotDropInAnimation(screenRect, showFlash);</span><br><span class="line">                <span class="comment">// 保存截图</span></span><br><span class="line">                saveScreenshotInWorkerThread(finisher, <span class="keyword">new</span> <span class="title class_">ActionsReadyListener</span> () &#123;</span><br><span class="line">                    <span class="meta">@Override</span></span><br><span class="line">                    <span class="keyword">void</span> <span class="title function_">onActionsReady</span> <span class="params">(SavedImageData imageData)</span> &#123;</span><br><span class="line">                        showUiOnActionsReady(imageData);</span><br><span class="line">                        mScreenshotIng = <span class="literal">false</span>;</span><br><span class="line">                    &#125;</span><br><span class="line">                &#125;);</span><br><span class="line">                <span class="comment">// 播放快门声音以通知我们已截屏</span></span><br><span class="line">                mCameraSound.play(MediaActionSound.SHUTTER_CLICK);</span><br><span class="line">                <span class="keyword">if</span> (mScreenshotPreview.isAttachedToWindow()) &#123;</span><br><span class="line">                    mScreenshotPreview.setLayerType(View.LAYER_TYPE_HARDWARE, <span class="literal">null</span>);</span><br><span class="line">                    mScreenshotPreview.buildLayer();</span><br><span class="line">                &#125;</span><br><span class="line">                <span class="comment">// 开始执行动画</span></span><br><span class="line">                mScreenshotAnimation.start();</span><br><span class="line">            &#125;);</span><br><span class="line">            &#125;);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 创建一个新的工作线程并将屏幕截图保存到媒体存储</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">void</span> <span class="title function_">saveScreenshotInWorkerThread</span><span class="params">(</span></span><br><span class="line"><span class="params">            Consumer&lt;Uri&gt; finisher, <span class="meta">@Nullable</span> ActionsReadyListener actionsReadyListener)</span> &#123;</span><br><span class="line">        <span class="type">SaveImageInBackgroundData</span> <span class="variable">data</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">SaveImageInBackgroundData</span>();</span><br><span class="line">        data.image = mScreenBitmap;  <span class="comment">// native 层返回的 Bitmap</span></span><br><span class="line">        data.finisher = finisher;</span><br><span class="line">        data.mActionsReadyListener = actionsReadyListener;</span><br><span class="line">        <span class="keyword">if</span> (mSaveInBgTask != <span class="literal">null</span>) &#123;</span><br><span class="line">            <span class="comment">// just log success/failure for the pre-existing screenshot</span></span><br><span class="line">            <span class="comment">// 只需记录预先存在的屏幕截图的成功失败</span></span><br><span class="line">            mSaveInBgTask.setActionsReadyListener(<span class="keyword">new</span> <span class="title class_">ActionsReadyListener</span>() &#123;</span><br><span class="line">                <span class="meta">@Override</span></span><br><span class="line">                <span class="keyword">void</span> <span class="title function_">onActionsReady</span><span class="params">(SavedImageData imageData)</span> &#123;</span><br><span class="line">                    logSuccessOnActionsReady(imageData);</span><br><span class="line">                &#125;</span><br><span class="line">            &#125;);</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="comment">// 截图的一些信息存储在 SaveImageInBackgroundTask 中构建</span></span><br><span class="line">        mSaveInBgTask = <span class="keyword">new</span> <span class="title class_">SaveImageInBackgroundTask</span>(mContext, data);</span><br><span class="line">        mSaveInBgTask.execute();</span><br><span class="line">    &#125;</span><br></pre></td></tr></table></figure>
<p>到此截屏流程完毕,可以查看下截图的View的xml文件：global_screenshot.xml<br><strong>frameworks/base/packages/SystemUI/res/layout/global_screenshot.xml</strong></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br></pre></td><td class="code"><pre><span class="line">&lt;FrameLayout</span><br><span class="line">    xmlns:android=<span class="string">&quot;http://schemas.android.com/apk/res/android&quot;</span></span><br><span class="line">    android:id=<span class="string">&quot;@+id/global_screenshot_frame&quot;</span></span><br><span class="line">    android:layout_width=<span class="string">&quot;match_parent&quot;</span></span><br><span class="line">    android:layout_height=<span class="string">&quot;match_parent&quot;</span>&gt;</span><br><span class="line">    &lt;ImageView</span><br><span class="line">        android:id=<span class="string">&quot;@+id/global_screenshot_actions_background&quot;</span></span><br><span class="line">        android:layout_height=<span class="string">&quot;@dimen/screenshot_bg_protection_height&quot;</span></span><br><span class="line">        android:layout_width=<span class="string">&quot;match_parent&quot;</span></span><br><span class="line">        android:layout_gravity=<span class="string">&quot;bottom&quot;</span></span><br><span class="line">        android:alpha=<span class="string">&quot;0.0&quot;</span></span><br><span class="line">        android:src=<span class="string">&quot;@drawable/screenshot_actions_background_protection&quot;</span>/&gt;</span><br><span class="line">    &lt;!--截屏动画相关的View --&gt;</span><br><span class="line">    &lt;ImageView</span><br><span class="line">        android:id=<span class="string">&quot;@+id/global_screenshot_animated_view&quot;</span></span><br><span class="line">        android:layout_width=<span class="string">&quot;wrap_content&quot;</span></span><br><span class="line">        android:layout_height=<span class="string">&quot;wrap_content&quot;</span></span><br><span class="line">        android:layout_gravity=<span class="string">&quot;top|start&quot;</span></span><br><span class="line">        android:visibility=<span class="string">&quot;gone&quot;</span></span><br><span class="line">        android:elevation=<span class="string">&quot;@dimen/screenshot_preview_elevation&quot;</span></span><br><span class="line">        android:background=<span class="string">&quot;@drawable/screenshot_rounded_corners&quot;</span> /&gt;</span><br><span class="line">    &lt;ImageView</span><br><span class="line">        android:id=<span class="string">&quot;@+id/global_screenshot_flash&quot;</span></span><br><span class="line">        android:layout_width=<span class="string">&quot;match_parent&quot;</span></span><br><span class="line">        android:layout_height=<span class="string">&quot;match_parent&quot;</span></span><br><span class="line">        android:visibility=<span class="string">&quot;gone&quot;</span></span><br><span class="line">        android:elevation=<span class="string">&quot;@dimen/screenshot_preview_elevation&quot;</span></span><br><span class="line">        android:src=<span class="string">&quot;@android:color/white&quot;</span>/&gt;</span><br><span class="line">    &lt;com.android.systemui.screenshot.ScreenshotSelectorView</span><br><span class="line">        android:id=<span class="string">&quot;@+id/global_screenshot_selector&quot;</span></span><br><span class="line">        android:layout_width=<span class="string">&quot;match_parent&quot;</span></span><br><span class="line">        android:layout_height=<span class="string">&quot;match_parent&quot;</span></span><br><span class="line">        android:visibility=<span class="string">&quot;gone&quot;</span></span><br><span class="line">        android:pointerIcon=<span class="string">&quot;crosshair&quot;</span>/&gt;</span><br><span class="line">    &lt;!-- 此处包含了一个layout, 而缩略图的View就在此layout中，</span><br><span class="line">         截屏右上角的关闭缩略图按钮 也在此layout中 --&gt;</span><br><span class="line">    &lt;include layout=<span class="string">&quot;@layout/global_screenshot_static&quot;</span>/&gt;</span><br><span class="line">&lt;/FrameLayout&gt;</span><br></pre></td></tr></table></figure>


      
      <blockquote>
        
        <strong>本文链接：</strong><br><a href="http://longzhiye.top/2023/12/16/2023-12-16/">http://longzhiye.top/2023/12/16/2023-12-16/</a>
      </blockquote>
    </div>
    <footer class="mdui-card-actions">
      
        <a class="mdui-ripple article_categories-link" href="/categories/%E6%8A%80%E6%9C%AF/">技术</a>
      
      
        <a class="mdui-ripple article_tags-link" href="/tags/Android/" rel="tag">Android</a><a class="mdui-ripple article_tags-link" href="/tags/Launcher/" rel="tag">Launcher</a><a class="mdui-ripple article_tags-link" href="/tags/SystemUI/" rel="tag">SystemUI</a><a class="mdui-ripple article_tags-link" href="/tags/framework/" rel="tag">framework</a>
      
    </footer>
    
  </article>
  
<script src="//cdn.bootcdn.net/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="//cdn.bootcdn.net/ajax/libs/fancybox/3.5.7/jquery.fancybox.min.js"></script>

  <script>$("#main article .mdui-card-content img.fancybox").on("click",function(e){$.fancybox.open({src:$(this).attr("src")});});</script>


  <nav id="paginator">
    
      <a rel="prev" class="extend prev" href="/2023/12/17/2023-12-17/">
        <button aria-label="prev" class="mdui-btn mdui-btn-raised mdui-btn-icon mdui-color-theme-accent mdui-ripple"><i class="mdui-icon material-icons">arrow_back</i></button>
        <span class="mdui-p-x-3" mdui-tooltip="{content: 'Android 事件分发机制详解（上）'}">上一篇</span>
      </a>
    
    <div class="spacer"></div>
    
      <a rel="next" class="extend next" href="/2023/12/10/2023-12-10/">
        <span class="mdui-p-x-3" mdui-tooltip="{content: 'Android10.0 人脸解锁流程分析'}">下一篇</span>
        <button aria-label="next" class="mdui-btn mdui-btn-raised mdui-btn-icon mdui-color-theme-accent mdui-ripple"><i class="mdui-icon material-icons">arrow_forward</i></button>
      </a>
    
  </nav>





</main>
  <footer id="footer" class="mdui-m-t-5 mdui-p-y-3 mdui-color-theme">
  <div class="mdui-p-y-0 mdui-text-center">
    
    
    
    
    
    
    
      <a href="http://github.com/longzhiye" target="_blank" class="mdui-btn mdui-btn-icon mdui-text-color-theme-a100"><i class="mdui-icon iconfont">&#xe7ab;</i></a>
    
    
    
      <a href="https://www.zhihu.com/people/long-zhi-xie-61" target="_blank" class="mdui-btn mdui-btn-icon mdui-text-color-theme-a100"><i class="mdui-icon iconfont">&#xe6c0;</i></a>
    
    
    
      <a href="tencent://tencent:////message/?uin=951898105&Menu=yes" target="_blank" class="mdui-btn mdui-btn-icon mdui-text-color-theme-a100"><i class="mdui-icon iconfont">&#xe651;</i></a>
    
    
  </div>
  <div class="mdui-p-y-1 mdui-text-center">
    Copyright &copy; 2022 - 2025 龙之叶<br>
    Powered by <a href="https://hexo.io/" target="_blank" class="mdui-text-color-theme-accent">Hexo</a>
    <br>
      <span id="busuanzi_container_site_pv" style="display: none;"><i class="iconfont">&#xe7fd;</i> <span id="busuanzi_value_site_pv"></span></span> &nbsp;&nbsp;
      <span id="busuanzi_container_site_uv" style="display: none;"><i class="iconfont">&#xe601;</i> <span id="busuanzi_value_site_uv"></span></span>
    
  </div>
</footer>

  <button id="gotop" class="mdui-fab mdui-fab-fixed mdui-fab-hide mdui-color-theme-accent mdui-ripple"><i class="mdui-icon material-icons">arrow_upward</i></button>
  <script async src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>
  
<script src="/js/mdui.js"></script>
<script src="/js/script.js"></script>

  
<script src="/custom.js"></script>

</body>
</html>
